<template>
  <div class="email">
    <!--S表单-->
    <el-form
      ref="settingForm"
      class="setting-form"
      :rules="formDataRules"
      :model="formData"
      label-position="top"
      label-width="100px"
      style="width: 500px;"
    >
      <el-alert
        :closable="false"
        title="仅支持QQ邮箱"
        type="success">
      </el-alert>
      <el-form-item label="系统邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="系统邮箱,仅支持QQ邮箱" />
      </el-form-item>
      <el-form-item label="系统邮箱显示用户名" prop="email_username">
        <el-input v-model="formData.email_username" placeholder="系统邮箱显示用户名" />
      </el-form-item>
      <el-form-item label="邮箱授权码" prop="email_password">
        <el-input v-model="formData.email_password" placeholder="邮箱授权码" />
      </el-form-item>
      <el-button type="primary" size="small" @click="submit">确认配置</el-button>
    </el-form>
    <!--E表单-->
  </div>
</template>

<script>
export default {
  name: 'Subscribe',
  props: {
    setting: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        email: '',
        email_username: '',
        email_password: ''
      },
      formDataRules: {
        email: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { type: 'email', message: '请设置正确的邮箱', trigger: 'blur' }
        ],
        email_username: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        email_password: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    setting() {
      const { email, email_username, email_password } = this.setting
      this.formData = {
        email,
        email_username,
        email_password
      }
    }
  },
  methods: {
    submit() {
      this.$refs.settingForm.validate((valid) => {
        if (valid) {
          this.$put('setting/message', this.formData)
            .then(() => {
              this.$message.success('修改成功')
            })
            .catch((error) => {
              this.$handleError(error)
            })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
